package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.annotations.LogOperation;
import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.User;
import cn.kgc.springboot.vue.enums.OperationType;
import cn.kgc.springboot.vue.form.UserForm;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.response.ResponseStatus;
import cn.kgc.springboot.vue.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author 课工场
 * @date 2024/12/18
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    // 认证
    @PostMapping("login")
    public ResponseResult login(@RequestBody HashMap<String, String> params,HttpServletRequest request){

        return userService.login(params,request);
    }


    @GetMapping("personalInfo")
    public ResponseResult getPersonalInfo(HttpServletRequest request){
        String token  = request.getHeader("authentication");

       return userService.getPersonalInfo(token);
    }

    @PutMapping("editPersonalPwd")
    public ResponseResult editPersonalPwd(HttpServletRequest request,@RequestBody HashMap<String,Object> params){
        String token  = request.getHeader("authentication");
        return userService.editPersonalPwd(token,params);

    }

    @GetMapping("logout")
    public ResponseResult  logout(HttpServletRequest request){
        String token  = request.getHeader("authentication");
      return   userService.logout(token);
    }


    @PutMapping("editPersonalInfo")
    public ResponseResult editPersonalInfo(@RequestBody HashMap<String,Object> params){
        return userService.editPersonalInfo(params);

    }

    @RequiresPermissions("sys:user:select")
    @PostMapping("page")
    public ResponseResult userPage(@RequestBody QueryCondition<User> queryCondition){

       return userService.userPage(queryCondition);
    }

    @RequiresPermissions("sys:user:add")
    @PostMapping("add")
    public ResponseResult addUser(@RequestBody UserForm userForm){

        return userService.addUser(userForm);
    }

    @RequiresPermissions("sys:user:update")
    @PutMapping("edit")
    public ResponseResult editUser(@RequestBody UserForm userForm){

        return userService.editUser(userForm);
    }



    @RequiresPermissions("sys:user:pwd")
    @PutMapping("resetPwd/{uid}")
    public ResponseResult resetPassword(@PathVariable Integer uid){

        return userService.resetPassword(uid);
    }


    @LogOperation(model = "用户模块",operation = OperationType.UPDATE)
    @RequiresPermissions("sys:user:state")
    @PutMapping("changeUserState")
    public ResponseResult changeUserState(@RequestBody HashMap<String,Integer> params){

        return userService.changeUserState(params);
    }


    @LogOperation(model = "用户模块",operation = OperationType.DELETE)
    @RequiresPermissions("sys:user:delete")
    @DeleteMapping("delete/{ids}")
    public ResponseResult deleteUsers(@PathVariable String ids){
        return userService.deleteUsers(ids);
    }


    @GetMapping("menu")
    public ResponseResult createUserMenu(HttpServletRequest request){

       return   userService.createUserMenu(request);
    }


    @GetMapping("unAuth")
    public ResponseResult unAuth(){

        return  ResponseResult.fail()
                .code(ResponseStatus.UN_AUTH)
                .message(ResponseStatus.UN_AUTH.getMessage());
    }

}
